﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;

namespace TP2
{
    public partial class AddRate : Form
    {
        RateForm rateFr;
        public AddRate(RateForm rate)
        {
            this.rateFr = rate;
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddCreditRate(string con_string, string client)
        {
            try
            {
                OracleConnection connection = new OracleConnection(con_string);
                connection.Open();
                string command = "insert into credit_rate values(0,'" + textBoxTitle.Text + "'," + (textBoxProcent.Text) + "," + textBoxPeriod.Text + ",'" + client + "')";
                OracleCommand insert = new OracleCommand(command, connection);
                insert.ExecuteNonQuery();
                connection.Close();
                this.Close();
            }
            catch
            {
                MessageBox.Show("ошибка во входных данных");
            }
            

        }

       
        private void AddAccumulationRate(string con_string, string client)
        {
            try
            {
                OracleConnection connection = new OracleConnection(con_string);
                connection.Open();

                string command = "insert into Accumulation_rate values(0,'" + textBoxTitle.Text + "'," + (textBoxProcent.Text) + "," + textBoxPeriod.Text + ",'" + client + "')";
                OracleCommand insert = new OracleCommand(command, connection);
                insert.ExecuteNonQuery();
                connection.Close();
                this.Close();
            }
            catch { MessageBox.Show("ошибка во входных данных"); }
        }        

        private void btnOk_Click(object sender, EventArgs e)
        {
            DBWorkClass dbWork = new DBWorkClass();
            string title = textBoxTitle.Text;
            int period = Convert.ToInt32(textBoxPeriod.Text);
            Double procent = Convert.ToDouble(textBoxProcent.Text);

            
            if (comboBoxOperation.SelectedIndex == 0) 
            {
                
                switch (comboBoxClient.SelectedIndex)
                {
                    case 0:
                        string client = "физический";
                        dbWork.InsertRate(title, procent, period, client, comboBoxOperation.SelectedIndex);
                    //this.AddCreditRate(con_string, client);
                        break;
                    case 1:
                         client = "юридический";
                         dbWork.InsertRate(title, procent, period, client, comboBoxOperation.SelectedIndex);
                        //this.AddCreditRate(con_string, client);
                        break;
                    case 2:
                         client = "банк";
                         dbWork.InsertRate(title, procent, period, client, comboBoxOperation.SelectedIndex);
                        //this.AddCreditRate(con_string, client);
                        break;
                }
            }
            else 
            {
                switch (comboBoxClient.SelectedIndex)
                {
                    case 0:
                        string client = "физический";
                        dbWork.InsertRate(title, procent, period, client, comboBoxOperation.SelectedIndex);
                        //this.AddAccumulationRate(con_string, client);
                        break;
                    case 1:
                        client = "юридический";
                        dbWork.InsertRate(title, procent, period, client, comboBoxOperation.SelectedIndex);
                        //this.AddAccumulationRate(con_string, client);
                        break;
                    case 2:
                        MessageBox.Show("такой услуги нету в нашем банке!");
                        break;
                }
            }
            rateFr.Refresh();
            this.Close();
        }
    }
}
